package exercise;

import java.util.Scanner;

public class EX02_AccountService implements EX02_IAccountService{
	
	private static EX02_IAccountDAO adao = null;
	
	public EX02_AccountService() {
		adao = new EX02_AccountDAO();
	}
	

	@Override
	public float getMonthlyInterestRate(EX02_AccountVO a) {				
		return a.getInterestRate()/12F;
	}
	@Override
	public int withDraw(EX02_AccountVO a, int money) {
		a.setBalance(a.getBalance()-money);
		return a.getBalance();
	}

	@Override
	public int deposit(EX02_AccountVO a, int money) {
		a.setBalance(a.getBalance()+money);
		return a.getBalance();
	}
	@Override
	public void createAccount() {
		EX02_AccountVO a = new EX02_AccountVO();		
		Scanner sc = new Scanner(System.in);
		System.out.println("계좌번호를 입력하세요");
		a.setAid(sc.next());
		System.out.println("잔액을 입력하세요");
		a.setBalance(sc.nextInt());
		System.out.println("연간 이자율을 입력하세요");
		a.setInterestRate(sc.nextFloat());		
		
		if (adao.insertAccount(a)) {
			System.out.println("계좌가 생성되었습니다.");
		} else {
			System.out.println("계좌 생성에 실패하였습니다.");
		}
	}
	@Override
	public void readAccount() {		
		System.out.println("==========================================================");
		for (EX02_AccountVO a :adao.selectAccount()) {
			System.out.print(a.getAid());
			System.out.print("\t");
			System.out.print(a.getBalance());
			System.out.print("\t");
			System.out.print(a.getInterestRate());
			System.out.print("\t");
			System.out.println(a.getDateCreated());
		}
		
	}	
	@Override
	public void removeAccount() {
		this.readAccount();
		EX02_AccountDAO adao = new EX02_AccountDAO();
		EX02_AccountVO a = new EX02_AccountVO();
		Scanner sc = new Scanner(System.in);
		System.out.println("삭제할 계좌번호를 입력하세요");
		a.setAid(sc.next());
		
		if (adao.deleteAccount(a)) {
			System.out.println("계좌가 삭제 되었습니다.");
		} else {
			System.out.println("계좌 삭제에 실패하였습니다.");
		}
		
	}
	@Override
	public void withDrawAccount() {
		this.readAccount();		
		EX02_AccountVO a = new EX02_AccountVO();
		Scanner sc = new Scanner(System.in);
		System.out.println("인출할 계좌번호를 입력하세요");
		a.setAid(sc.next());
		System.out.println("인출 금액을 입력하세요");
		this.withDraw(a, sc.nextInt());
		a.setBalance(a.getBalance());	
		
//		System.out.println(a.getBalance());
		
		if (adao.updateAccount(a)) {
			System.out.println("인출 되었습니다.");
		} else {
			System.out.println("인출에 실패하였습니다.");
		}
	}
	@Override
	public void depositAccount() {
		this.readAccount();
		EX02_AccountDAO adao = new EX02_AccountDAO();
		EX02_AccountVO a = new EX02_AccountVO();
		Scanner sc = new Scanner(System.in);
		System.out.println("입금할 계좌번호를 입력하세요");
		a.setAid(sc.next());
		System.out.println("입금 금액을 입력하세요");
		this.deposit(a, sc.nextInt());
		a.setBalance(a.getBalance());		
		
		if (adao.updateAccount(a)) {
			System.out.println("입급 되었습니다.");
		} else {
			System.out.println("입금에 실패하였습니다.");
		}
	}

	
	public void menu(){
		while (true) {
			System.out.println("=== 계좌 관리 ===");
			System.out.println("1. 계좌 생성");
			System.out.println("2. 전체 계좌 조회");
			System.out.println("3. 입금");
			System.out.println("4. 인출");
			System.out.println("5. 계좌 삭제");
			System.out.println("0. 종료");
			System.out.println("=============");
			System.out.println("메뉴 번호를 입력하세요.");
			Scanner sc = new Scanner(System.in);
			switch (sc.nextInt()){
			case 0:
				System.exit(0);
			case 1:
				this.createAccount(); break;
			case 2:
				this.readAccount(); break;
			case 3:
				this.depositAccount(); break;
			case 4:
				this.withDrawAccount(); break;
			case 5:
				this.removeAccount(); break;
				default :
					System.out.println("올바른 번호를 입력하세요.");
			}
		}
	}
}
